<?php

/**
 * base action
 *
 * @author hzhi <hzhilamp@163.com>
 */
class BaseAction extends Action
{
    //请求数据的客户端
    protected $restClient;

    //基础URL
    protected $baseUrl;

    //客户端生成sign
    protected $sign;


    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();

        $this->baseUrl = $this->getBaseUrl();

        //设置请求资源
        $this->restClient = $this->restClient(C('REQUEST_URL'));

        //客户端生成sign
        $this->sign = $this->createSign();

    }

    /**
     * 生成加密码
     *
     * @return string
     */
    public function createSign()
    {
        $crypt = new Crypt();
        $restSign = new RestSign();
        $authConfig = C('SOURCE_GROUP');
        $sign = $crypt->encrypt(
            $restSign->setConfig($authConfig)
                ->setSign(), $restSign->getKey(), true
        );

        return $sign;

    }

    /**
     * 实例化JsonClient
     *
     * @param string $baseUrl 发起请求的地址
     *
     * @return JsonClient
     */
    public function restClient($baseUrl)
    {
        return new JsonClient($baseUrl);
    }

    /**
     * 获取IP段
     *
     * @param string $ip IP信息
     *
     * @return array
     */
    public function getLocationIp($ip)
    {
        $ipLocation = new IpLocation();

        return $ipLocation->getlocation($ip);

    }

    /**
     * 获取分页每页条数
     *
     * @return mixed
     */
    public function getPerPage()
    {
        return C('PER_PAGE');
    }

    /**
     * 获取项目基本地址
     *
     * @return mixed
     */
    public function getBaseUrl()
    {
        return C('BASE_URL');
    }

    /**
     * 获取操作的控制器和方法
     *
     * @return string
     */
    protected function getControllerFunction()
    {
        $action = $this->getActionName();
        //return $action.'/'.ACTION_NAME;
        return $action;
    }


    protected function upload($filename)
    {
        //上传图片的最大尺寸，单位kb
        $fileMaxSize = 10000;

        $base = C('UPLOAD_PATH');

        try {

            $uploader = new Upload($filename);

            //检测是不是图片格式文件
            if (!$uploader->isImage()) {
                $result = array('result' => 0, 'message' => "请选择图片格式");

            } elseif (!($uploader->getSize() < $fileMaxSize)) {
                $result = array('result' => 0, 'message' => "请选择文件大小{$fileMaxSize}k以内的图片");

            } else {
                $savepath = $uploader->setBasePath($base)->save();
                $result = array('result' => 1, 'message' => '上传成功' , 'destFile'=>'/Public/uploads/img/' . $savepath);
            }

        } catch (\Exception $e) {
            if ($e->getMessage() == 'not file selected') {
                $result = array('result' => 0, 'message' => "请选择图片上传");
            }
            if ($e->getMessage() == 'file size too large') {
                $result = array('result' => 0, 'message' => "上传文件太大了,请重新选择!");
            }
        }
        return $result;

    }

    protected function _message($status, $message, $url)
    {
        $result = array('status'=>$status, 'message'=>$message, 'url'=>$url);
        return json_encode($result);
    }

    /**
     * 获取参数并且安全过滤
     * @param string $name   要获取的参数名称
     * @param string $filter 检测模式 严格:strict 普通:normal 不检测:none
     * @return array|mixed|null|string
     */
    protected function getParameter($name, $filter = 'strict')
    {
        $data = I($name);
        if ($data !== null) {
            if (!is_array($data)) {
                $data = $this->securityFilter($data, $filter);
            } else {
                $data = $this->arraySecurityFilter($data, $filter);
            }
        }
        return $data === '' ? null : $data;

    }

    /**
     * @param $str 需要检测的字串
     * @param $filter  检测模式 严格:strict 普通:normal 不检测:none
     * @return string
     */
    private static function securityFilter($str, $filter)
    {
        if (!is_numeric($str)) {
            $charset = mb_check_encoding($str, 'UTF-8') ? 'UTF-8' : 'GB2312';
            $str = preg_replace('/ed2k.*?document.write/is', '', $str);
            switch ($filter) {
                case 'strict':
                    $str = htmlspecialchars(stripslashes($str), ENT_QUOTES, $charset);
                    break;
                case 'normal':
                    $str = htmlspecialchars($str, ENT_QUOTES, $charset);
                    break;
                case 'none':
                    break;
            }
        }
        return $str;
    }

    /**
     * 递归检测的数组字符串
     * @param array $data 需要转换的数组数据
     * @param string $filter   检测模式 严格:strict 普通:normal 不检测:none
     * @return array|null 转换后的数组,如果$data不是数组返回null
     */
    private static function arraySecurityFilter($data, $filter)
    {
        if (!is_array($data)) {
            return null;
        }
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $data[$k] = self::arraySecurityFilter($v, $filter);
            } else {
                $data[$k] = self::securityFilter($v, $filter);
            }
        }
        return $data;
    }
}
